﻿package com.moonshineproject.plugin.actionscript.as3project.vo 
{
	import __AS3__.vec.Vector;
	
	import com.moonshineproject.plugin.actionscript.as3parser.listener.ParserController;
	import com.moonshineproject.plugin.actionscript.as3project.exporter.FlashDevelopExporter;
	import com.moonshineproject.plugin.actionscript.as3project.settings.PathListSetting;
	import com.moonshineproject.plugin.project.vo.ProjectVO;
	import com.moonshineproject.plugin.settings.vo.BooleanSetting;
	import com.moonshineproject.plugin.settings.vo.ColorSetting;
	import com.moonshineproject.plugin.settings.vo.ISetting;
	import com.moonshineproject.plugin.settings.vo.IntSetting;
	import com.moonshineproject.plugin.settings.vo.MultiOptionSetting;
	import com.moonshineproject.plugin.settings.vo.NameValuePair;
	import com.moonshineproject.plugin.settings.vo.PathSetting;
	import com.moonshineproject.plugin.settings.vo.SettingsWrapper;
	import com.moonshineproject.plugin.settings.vo.StringSetting;
	
	import flash.filesystem.File;
	
	public class AS3ProjectVO extends ProjectVO 
	{
		public static const TEST_MOVIE_EXTERNAL_PLAYER:String = "ExternalPlayer";
		public static const TEST_MOVIE_CUSTOM:String = "Custom";
		public static const TEST_MOVIE_OPEN_DOCUMENT:String = "OpenDocument";
		public static const TEST_MOVIE_AIR:String = "AIR";
		
		public var fromTemplate:File;
		
		public var projectName:String;
		
		public var parserController:ParserController;
		
		public var swfOutput:SWFOutputVO;
		public var buildOptions:BuildOptions;
		
		public var classpaths:Vector.<File> = new Vector.<File>();
		public var includeLibraries:Vector.<File> = new Vector.<File>();
		public var libraries:Vector.<File> = new Vector.<File>();
		public var externalLibraries:Vector.<File> = new Vector.<File>();
		public var runtimeSharedLibraries:Vector.<File> = new Vector.<File>();
		public var intrinsicLibraries:Vector.<File> = new Vector.<File>();
		public var assetLibrary:XMLList; // TODO Unknown if it works in FD, there just for compatibility purposes (<library/> tag)
		public var targets:Vector.<File> = new Vector.<File>();
		public var hiddenPaths:Vector.<File> = new Vector.<File>();
		public var showHiddenPaths:Boolean = false;
		
		public var prebuildCommands:String;
		public var postbuildCommands:String;
		public var postbuildAlways:Boolean;
		
		public var testMovie:String = TEST_MOVIE_EXTERNAL_PLAYER;
		public var testMovieCommand:String;
		
		public var config:MXMLCConfigVO;
		
		public function get air():Boolean
		{
			if (buildOptions && buildOptions.additional)
			{
				return (buildOptions.additional.indexOf("+configname=air") != -1);
			}
			return false;	
		}
		public function set air(v:Boolean):void
		{
			if (air) return;
			
			if (v && buildOptions && buildOptions.additional)
			{
				if (buildOptions.additional.length > 0) buildOptions.additional += " ";
				buildOptions.additional += "+configname=air";
				this.testMovie = TEST_MOVIE_AIR;
			}
		}
		
		override public function get name():String
		{
			return projectName;
		}
		
		protected var configInvalid:Boolean = true;
		
		
		
		public function AS3ProjectVO(folder:File) 
		{
			super(folder);
			
			swfOutput = new SWFOutputVO();
			buildOptions = new BuildOptions();
			
			config = new MXMLCConfigVO();
		}
		
		override public function getSettings():Vector.<SettingsWrapper>
		{
			

			// TODO more categories / better setting UI
			var settings:Vector.<SettingsWrapper> = Vector.<SettingsWrapper>([
				new SettingsWrapper("Output", 
					Vector.<ISetting>([
						new IntSetting(swfOutput,	"frameRate", 	"Framerate (FPS)"),
						new IntSetting(swfOutput,	"width", 		"Width"),
						new IntSetting(swfOutput,	"height",	 	"Height"),
						new ColorSetting(swfOutput,	"background",	"Background color"),
						new IntSetting(swfOutput,	"swfVersion",	"Minimum player version")
					])
				),
				new SettingsWrapper("Build options",
					Vector.<ISetting>([
						new PathSetting(buildOptions, "customSDKPath", "Custom SDK", true, buildOptions.customSDKPath),
						new StringSetting(buildOptions, "additional", "Additional compiler options"),
						
						new StringSetting(buildOptions, "compilerConstants",				"Compiler constants"),
						
						new BooleanSetting(buildOptions, "accessible",						"Accessible SWF generation"),
						new BooleanSetting(buildOptions, "allowSourcePathOverlap",			"Allow source path overlap"),
						new BooleanSetting(buildOptions, "benchmark",						"Benchmark"),
						new BooleanSetting(buildOptions, "es",								"ECMAScript edition 3 prototype based object model (es)"),
						new BooleanSetting(buildOptions, "optimize",						"Optimize"),
						
						new BooleanSetting(buildOptions, "useNetwork",						"Enable network access"),
						new BooleanSetting(buildOptions, "useResourceBundleMetadata",		"Use resource bundle metadata"),
						new BooleanSetting(buildOptions, "verboseStackTraces",				"Verbose stacktraces"),
						new BooleanSetting(buildOptions, "staticLinkRSL",					"Static link runtime shared libraries"),
						
						new StringSetting(buildOptions, "linkReport",						"Link report XML file"),
						new StringSetting(buildOptions, "loadConfig",						"Load config")
					])
				),
				new SettingsWrapper("Paths",
					Vector.<ISetting>([
						new PathListSetting(this, "classpaths", "Class paths", folder, false),
						new PathListSetting(this, "externalLibraries", "External libraries", folder, true, false),
						new PathListSetting(this, "libraries", "Libraries", folder)
					])
				),
				new SettingsWrapper("Warnings & Errors",
					Vector.<ISetting>([
						new BooleanSetting(buildOptions, "showActionScriptWarnings",		"Show actionscript warnings"),
						new BooleanSetting(buildOptions, "showBindingWarnings",				"Show binding warnings"),
						new BooleanSetting(buildOptions, "showDeprecationWarnings",			"Show deprecation warnings"),
						new BooleanSetting(buildOptions, "showUnusedTypeSelectorWarnings",	"Show unused type selector warnings"),
						new BooleanSetting(buildOptions, "warnings",						"Show all warnings"),
						new BooleanSetting(buildOptions, "strict",							"Strict error checking"),
					])
				),
				new SettingsWrapper("Run",
					Vector.<ISetting>([
						new MultiOptionSetting(this, 'testMovie', 							"Launch", 
							Vector.<NameValuePair>([
								new NameValuePair("AIR", TEST_MOVIE_AIR),
								new NameValuePair("Custom", TEST_MOVIE_CUSTOM),
								new NameValuePair("Open with default application", TEST_MOVIE_OPEN_DOCUMENT)
							])
						),
						new StringSetting(this, 'testMovieCommand', 						"Custom launch command")
					])
				)
			]);
			
			return settings;
		}
		
		override public function saveSettings():void
		{
			// Figure out which one is the settings file
			var settingsFile:File = folder.resolvePath(projectName+".as3proj");
			
			// Write settings
			FlashDevelopExporter.export(this, settingsFile);
		}
		
		
		public function updateConfig():void 
		{
			if (configInvalid)
			{
				config.write(this);
				configInvalid = false;
			}
		}
	}
}